package com.skyz.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.luck.picture.lib.utils.ToastUtils;
import com.skyz.base.util.EventBusUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


public abstract class BaseFragment extends Fragment {

    protected final String TAG = getClass().getSimpleName();
    //当前fragment 的Context对象实例
    protected Context mContext;

    //AndroidX 的setUserVisibleHint方法被标注为Deprecated,懒加载需要使用setMaxLifecycle实现。
    //具体请使用BaseFragmentPagerAdapter实现
    //当前fragment 在需要懒加载的情况下 是否懒加载过
    private boolean mLoaded = false;

    private View mContentView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtils.register(this);
    }

    @Override
    public void onDestroy() {
        if (EventBusUtils.isRegistered(this)) {
            EventBusUtils.unregister(this);
        }
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEvent(Object event) {
        onMessageEvent(event);
    }

    public abstract void onMessageEvent(Object event);

    @Override
    public void onResume() {
        super.onResume();
        tryLazyLoad();
    }

    private void tryLazyLoad() {
        //需要懒加载  //没有加载过
        if (isLazyLoad() && !mLoaded) {
            Log.i(TAG, TAG + " lazyLoadData");
            lazyLoadData(mContentView);
            mLoaded = true;
        }
    }

    //当前fragment 是否需要懒加载
    protected boolean isLazyLoad() {
        return true;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //onCreateView 方法可能会被多次调用（Fragment 生命周期)
        mContext = inflater.getContext();
        if (mContentView == null) {
            initContentView(inflater, container, savedInstanceState);
            initData(savedInstanceState);
        } else {
            ViewGroup viewGroup = (ViewGroup) mContentView.getParent();
            if (viewGroup != null) {
                viewGroup.removeView(mContentView);
            }
        }
        return mContentView;
    }

    private void initContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //获取到 子类Activity 的布局id，并将布局加载进来
        int layoutId = getLayoutId();
        if (layoutId == 0) {
            Log.w(TAG, "You may forget to fill in the layout id!");
            return;
        }
        mContentView = inflater.inflate(layoutId, container, false);
        onLayoutInflated(mContentView);
    }

    protected abstract int getLayoutId();

    protected abstract void onLayoutInflated(View view);

    protected abstract void initData(Bundle savedInstanceState);

    protected abstract void lazyLoadData(View view);


}
